package IOBaseTest;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by yexuan on 2018/3/12.
 * 自定义readLine方法
 */
class MyRead{
    private FileReader fr;

    public MyRead(FileReader fr) {
        this.fr = fr;
    }

    public String myReadLine() throws IOException {
        StringBuilder sb = new StringBuilder();
        int num = 0;
        while((num = fr.read())!=-1){
            if(num=='\r')
                continue;
            if(num=='\n')
                return sb.toString();
            else
                sb.append((char)num);
        }
        if(sb.length()!=0)
            return sb.toString();
        return null;
    }

    public void myClose() throws IOException {
        fr.close();
    }
}
public class MyReadLineDemo {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("buff.txt");
        MyRead mr = new MyRead(fr);

        String line = null;
        while ((line=mr.myReadLine())!=null)
            System.out.println(line);

        mr.myClose();
    }
}
